package com.mfangsoft.zhuangjialong.integration.newActivity.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.incrementer.H2SequenceMaxValueIncrementer;
import org.springframework.stereotype.Service;

import com.mfangsoft.zhuangjialong.common.context.UserContext;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.integration.newActivity.mapper.NewActivityMapper;
import com.mfangsoft.zhuangjialong.integration.newActivity.service.INewActivityService;
import com.mfangsoft.zhuangjialong.integration.user.model.UserEntity;

@Service
public class NewActivityServiceImpl implements INewActivityService {
	
	@Autowired
	NewActivityMapper newActivityMapper;

	@Override
	public void addActivityTemplate(Map<String, Object> map) {
		List<Integer> list = (List<Integer>) map.get("filialeIds");
		if(list!=null && list.size()>0){
			map.put("shops",list.size());
		}
		newActivityMapper.addActivityTemplate(map);
		if(list!=null && list.size()>0){
			for (Integer integer : list) {
				map.put("user_id", integer);
				String code = createCode();
				map.put("activity_code", code);
				newActivityMapper.addActivity(map);
				String s = map.get("buy_money")+"";
				if(s!=null && !"".equals(s)){
					map.put("itme_id", 1);
					newActivityMapper.addActivityContent(map);
				}
			}
		}
	}

	@Override
	public void updateActivityTemplate(Map<String, Object> map) {
		List<Integer> list = (List<Integer>) map.get("filialeIds");
		if(list!=null && list.size()>0){
			map.put("shops",list.size());
		}else{
			map.put("shops",0);
		}
		newActivityMapper.updateActivityTemplate(map);
		newActivityMapper.delectActivity(map);
		newActivityMapper.deleteActivityContent(map);
		if(list!=null && list.size()>0){
			for (Integer integer : list) {
				map.put("user_id", integer);
				String code = createCode();
				map.put("activity_code", code);
				newActivityMapper.addActivity(map);
			}
		}
	}

	@Override
	public void updateActivity(Map<String, Object> map) {
		List<Integer> shopIds = (List<Integer>) map.get("shopIds");
		if(shopIds!=null && shopIds.size()>0){
			map.put("shops", shopIds.size());
		}else{
			map.put("shops", 0);
		}
		newActivityMapper.updateActivity(map);
		newActivityMapper.deleteActivityContent(map);
		List<Map<String,Object>> list=(List<Map<String,Object>>)map.get("content");
		if(list!=null && list.size()>0){
			for(Map<String,Object> m:list){
				m.put("activity_id", map.get("activity_id"));
				newActivityMapper.addActivityContent(m);
			}
		}
		newActivityMapper.deleteActivityShop(Integer.valueOf(map.get("activity_id").toString()));
		if(shopIds!=null && shopIds.size()>0){
			for (Integer integer : shopIds) {
				Map<String,Object> m=new HashMap<>();
				m.put("shop_id", integer);
				m.put("activity_id", map.get("activity_id"));
				newActivityMapper.addActivityShop(m);
			}
		}
	}
	
	public String createCode(){
		String s=""+new Date().getTime();
		String code="HDM"+s.substring(s.length()-3,s.length())+Math.round((Math.random()*9+1) * 1000000);

		Integer activityCode = newActivityMapper.selectActivityCode(code);
		if(activityCode>0){
			return createCode();
		}else{
			return code;
		}
	}

	@Override
	public Page<Map<String,Object>> selectActivityTemplate(Page<Map<String,Object>> page) {
		page.setData(newActivityMapper.selectActivityTemplatePage(page));
		return page;
	}
	@Override
	public Page<Map<String,Object>> selectWeiFaBuPage(Page<Map<String,Object>> page) {
		Map<String,Object> map=(Map<String, Object>) page.getParam();
		UserEntity user = UserContext.getCurrentUser();
		if(user!=null){
			if(map!=null){
				map.put("user_id", user.getId());
				page.setParam(map);
			}else{
				Map<String,Object> m=new HashMap<>();
				m.put("user_id", user.getId());
				page.setParam(m);
			}
		}
		page.setData(newActivityMapper.selectWeiFaBuPage(page));
		return page;
	}

	@Override
	public List<Map<String, Object>> selectPublishList(Integer pid) {
		return newActivityMapper.selectPublishList(pid);
	}

	@Override
	public Map<String, Object> selectTemplateDetail(Integer p_id) {
		Map<String, Object> templateDetail = newActivityMapper.selectTemplateDetail(p_id);
		List<Map<String,Object>> templateFiliale = newActivityMapper.selectTemplateFiliale(p_id);
		if(templateFiliale!=null && templateFiliale.size()>0){
			templateDetail.put("templateFiliale", templateFiliale);
		}
		return templateDetail;
	}

	@Override
	public Map<String, Object> selectActivityDetail(Integer activity_id) {
		Map<String, Object> activityDetail = newActivityMapper.selectActivityDetail(activity_id);
		List<Map<String,Object>> activityContent = newActivityMapper.selectActivityContent(activity_id);
		if(activityContent!=null && activityContent.size()>0){
			for (Map<String, Object> map : activityContent) {
				String s=map.get("gift")+"";
				String[] split = s.split(",");
				if(split!=null && split.length>0){
					List<String> asList = Arrays.asList(split);
					Map<String,Object> m=new HashMap<>();
					m.put("gifts", asList);
					List<Map<String,Object>> gifts = newActivityMapper.selectGifts(asList);
					map.put("gifts", gifts);
				}
			}
		}
		List<Map<String,Object>> activityShop = newActivityMapper.selectActivityShop(activity_id);
		activityDetail.put("activityShop", activityShop);
		activityDetail.put("activityContent", activityContent);
		return activityDetail;
	}

	@Override
	public Page<Map<String, Object>> selectActivityPage(Page<Map<String, Object>> page) {
		UserEntity currentUser = UserContext.getCurrentUser();
		if(currentUser.getId() == 153){
			List<Map<String,Object>> KFZList = newActivityMapper.selectKFZActivityPage(page);
			page.setData(KFZList);
			return page;
		}
		if(currentUser.getUser_level() == 3){
			Map<String,Object> map = (Map<String,Object>)page.getParam();
			map.put("user_id",UserContext.getCurrentUserId());
			page.setParam(map);
			List<Map<String,Object>> ThreeList = newActivityMapper.selectThreeActivityPage(page);
			page.setData(ThreeList);
			return page;
		}
		if(currentUser.getUser_level() == 2){
			Map<String,Object> map = (Map<String,Object>)page.getParam();
			map.put("user_id",UserContext.getCurrentUserId());
			page.setParam(map);
			List<Map<String,Object>> TwoList = newActivityMapper.selectTwoActivityPage(page);
			page.setData(TwoList);
			return page;
		}
		if(currentUser.getUser_level() == 1){
			Map<String,Object> map = (Map<String,Object>)page.getParam();
			map.put("user_id",UserContext.getCurrentUserId());
			page.setParam(map);
			List<Map<String,Object>> OneList = newActivityMapper.selectOneActivityPage(page);
			page.setData(OneList);
			return page;
		}
		return page;
	}

	@Override
	public Page<Map<String, Object>> selectActivityApplyListPage(Page<Map<String, Object>> page) {
		List<Map<String,Object>> listPage = newActivityMapper.selectActivityApplyListPage(page);
		page.setData(listPage);
		return page;
	}

	@Override
	public Map<String, Object> selectActivityStatistics(Integer activity_id) {
		Map<String, Object> statistics = newActivityMapper.selectActivityStatistics(activity_id);
		return statistics;
	}


}
